firstnames = [
	"Василий",
	"Пётр",
	"Фёкл",
	"Дмитрий",
	"Иван",
	"Архип",
	"Денис",
	"Коловрат",
	"Степан",
	"Даша",
	"Зина",
	"Дуся",
	"Настя",
	"Света",
	"Катя",
	"Таня"
]

secondnames = [
	"Иванов",
	"Петров",
	"Сидоров",
	"Дьяченко",
	"Сусанин",
	"Шпак",
	"Гец",
	"Гуздец",
	"Путин",
	"Медведев",
	"Шевченко"
]

var query;
var unic_serials = {};

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

function IsUnic(serial, number) {
	for (var i = 0; i < serial.length; i++) {
		if (serial[i] == number) return false;
	};
	return true;
}

function getUnicRandomInt(serial, min, max) {
	var answer;
	do {
		answer = getRandomInt(min, max);
	} while (IsUnic(answer) == false);
	serial[serial.length] = answer;
	return answer;
}

function CreatePersonsTable() {
	return "CREATE TABLE IF NOT EXISTS `persons` (" + "<br>" +
	"`id` int(11) NOT NULL AUTO_INCREMENT," + "<br>" +
	"`nickname` varchar(20) NOT NULL," +  "<br>" +
	"`firstname` int(11)," + "<br>" +
	"`secondname` int(11)," + "<br>" +
	"PRIMARY KEY (`id`)" + "<br>" +
	") ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=17" + "<br>" + "<br>";
}

function MultiInsert(isLast, args) {
	var result = "(";
	var to_arg = args.length - 1;

	if (args.length == 0) return "";

	for (var i = 0; i < to_arg; i++) {
		if (typeof(args[i]) == "string")
			result += "`" + args[i] + "`, ";
		else result += args[i] + ",";
	};

	if (isLast)
		if (typeof(args[i]) == "string")
			result += "`" + args[i] + "`);" + "<br>";
		else result += args[i] + ");" + "<br>";
	else
		if (typeof(args[i]) == "string")
			result += "`" + args[i] + "`)," + "<br>";
		else result += args[i] + ")," + "<br>";
	return result;
}

function AutoMultiInsert(numrows) {
	var numargs = 0;
	var setof_inserts = [];
	var j;
	var result = "";

	for (var i = 0; i < numrows; i++) {
		j = 1;
		while (j != arguments.length) {
			switch (arguments[j]) {
				case "randomlist":
					setof_inserts[numargs++] = 
						arguments[j + 1][getRandomInt(0, arguments[j + 1].length-1)];
					j = j + 2;
					break
				case "seriallist":
					setof_inserts[numargs++] = arguments[j + 1][i];
					j = j + 2;
					break
				case "randomset":
					setof_inserts[numargs++] = 
						getRandomInt(arguments[j+1], arguments[j+2]);
					j += 3;
					break
				case "serialset":
					setof_inserts[numargs++] = arguments[j+1]+i;
					j += 2;
					break
			}
		}
		numargs = 0;
		if (i == numrows - 1) result += MultiInsert(true, setof_inserts);
		else result += MultiInsert(false, setof_inserts);
	}
	
	return result;
}

function FillPersonsTable() {
	var result, i;
	result = "INSERT INTO `persons` (`id`, `nickname`, `firstname`, `secondname`) VALUES" + "<br>";
	/*
	for (i = 0; i < 20; i++) {
		result += MultiInsert(false, [i, "spy", firstnames[getRandomInt(0, firstnames.length-1)],
			secondnames[getRandomInt(0, secondnames.length-1)]]);
	}
	result += MultiInsert(true, [i, "spy", firstnames[getRandomInt(0, firstnames.length-1)],
			secondnames[getRandomInt(0, secondnames.length-1)]]);*/
	return AutoMultiInsert(5, "serialset", 1, "randomset", 1, 10);
}

function Start() {
	query = "qqq<br>aaa";
	document.write(FillPersonsTable());
}

